package com.diyunapp.xilinapp.adapters;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.GoodsSortsBottomBean;
import com.diyunapp.xilinapp.bean.YouLikeGoodsBean;
import com.diyunapp.xilinapp.myapi.OnRecyclerViewItemClickListener;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by NIDAYE on 2016/6/15.
 */
public class RecycleViewSortsAdapter extends RecyclerView.Adapter<RecycleViewSortsAdapter.MyViewHolder> implements View.OnClickListener{
    private static final int TYPE_HEADER = 0, TYPE_ITEM = 1, TYPE_FOOT = 2;

    public List<GoodsSortsBottomBean> mDatas;
    private View headView;
    private View footView;
    private int headViewSize = 0;
    private int footViewSize = 0;
    private ChangeGridLayoutManagerSpance changeGridLayoutManager;
    private boolean isAddFoot=false;
    private boolean isAddHead=false;

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    //显示图片的配置
    DisplayImageOptions options ;

    public interface ChangeGridLayoutManagerSpance{
        public void change(int size, boolean isAddHead, boolean isAddFoot);
    }
    //提供接口给 让LayoutManager根据添加尾部 头部与否来做判断 显示头部与底部的SpanSize要在添加头部和尾部之后
    public void setChangeGridLayoutManager(ChangeGridLayoutManagerSpance changeGridLayoutManager){
        this.changeGridLayoutManager=changeGridLayoutManager;
        changeGridLayoutManager.change(getItemCount()-1,isAddHead,isAddFoot);
    }

    public RecycleViewSortsAdapter(List<GoodsSortsBottomBean> datas) {
        mDatas = datas;
        //显示图片的配置
        options =  new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.sort_default) // resource or drawable
                .showImageForEmptyUri(R.drawable.sort_default) // resource or drawable
                .showImageOnFail(R.drawable.sort_default) // resource or drawable
                .cacheInMemory()
                .cacheOnDisc()
                .build();
    }

    public void addHeadView(View view) {
        headView = view;
        headViewSize = 1;
        isAddHead=true;
    }

    public void addFootView(View view) {
        footView = view;
        footViewSize = 1;
        isAddFoot=true;
    }

    @Override
    public int getItemViewType(int position) {
        int type = TYPE_ITEM;

        if (headViewSize==1 && position == 0) {
            type = TYPE_HEADER;
        } else if (footViewSize==1 && position == getItemCount()-1) {
            //最后一个位置
            type = TYPE_FOOT;
        }
        return type;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        Log.i("zhangxiaoxidebug","MyViewHolder onCreateViewHolder");
        View view = null;
        switch (i) {
            case TYPE_HEADER:
                view = headView;
                break;

            case TYPE_ITEM:
                view = View.inflate(viewGroup.getContext(), R.layout.goodsort_bottom_list, null);
                view.setOnClickListener(this);
                break;

            case TYPE_FOOT:
                view =footView;
                break;
        }
        return new MyViewHolder(view,i);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        if(getItemViewType(position)==TYPE_ITEM)
        {
            Log.i("zhangxiaoxidebug","onBindViewHolder position="+position);
            int dataPosition = position - headViewSize;
            Log.i("zhangxiaoxidebug","onBindViewHolder dataPosition="+dataPosition);
            GoodsSortsBottomBean currentdata = mDatas.get(dataPosition);
            ImageLoader.getInstance().displayImage(currentdata.getSortImgUrl(), holder.sorts_picture_iv, options);
            holder.sorts_title_tv.setText(currentdata.getSortTitle());
            holder.sorts_detail_tv.setText(currentdata.getSortDetail());
            if(currentdata.isIfDisplay())
            {
                holder.selete_fl.setVisibility(View.VISIBLE);
                if(currentdata.isBeSelected())
                {
                    holder.beseleted_view.setVisibility(View.VISIBLE);
                    holder.unseleted_view.setVisibility(View.GONE);
                }
                else
                {
                    holder.unseleted_view.setVisibility(View.VISIBLE);
                    holder.beseleted_view.setVisibility(View.GONE);
                }
            }
            else
            {
                holder.selete_fl.setVisibility(View.GONE);
            }

            //将数据保存在itemView的Tag中，以便点击时进行获取
            holder.itemView.setTag(dataPosition);
        }
    }

    @Override
    public int getItemCount()
    {
        return mDatas.size() + headViewSize + footViewSize;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView sorts_picture_iv;
        TextView sorts_title_tv;
        TextView sorts_detail_tv;
        View unseleted_view;
        View beseleted_view;
        FrameLayout selete_fl;

        public MyViewHolder(View itemView,int type) {
            super(itemView);
            //当View是itemview时再执行此初始化操作
            Log.i("zhangxiaoxidebug","MyViewHolder out");
            if(type == TYPE_ITEM )
            {
                Log.i("zhangxiaoxidebug","MyViewHolder");
                sorts_picture_iv = (ImageView)itemView.findViewById(R.id.bottomelist_sorttype_img);
                sorts_title_tv =(TextView)itemView.findViewById(R.id.bottomelist_sorttitle_tv);
                sorts_detail_tv =(TextView)itemView.findViewById(R.id.bottomelist_sorttintroduce_tv);
                selete_fl  =(FrameLayout) itemView.findViewById(R.id.bottome_select_fl);
                unseleted_view =itemView.findViewById(R.id.bottome_unselected_line_view);
                beseleted_view =itemView.findViewById(R.id.bottom_selected_line_view);
            }

        }
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}



